<?php
namespace app\overlord\controller;
use app\AdminRest;

use app\overlord\model\Record;
use app\overlord\model\RefundOrder;
use app\restaurant\model\Store;
use think\App;

use app\overlord\model\Order as model;
;

use think\facade\Db;


class AdminOrder extends AdminRest
{

    protected $model;

    protected $store_model;

    protected $refund_order_model;

    protected $record_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model              = new Model();

        $this->store_model        = new Store();

        $this->refund_order_model = new RefundOrder();

        $this->record_model       = new Record();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 10:52
     * @功能说明:门店列表
     */
    public function storeList(){

        $input = $this->_input;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->store_model->storeList($dis,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //充值金额
                $v['order_fee']    = $this->model->getStoreFee($v['id']);
                //退款金额
                $v['refund_fee']   = $this->refund_order_model->getStoreFee($v['id']);
                //使用金额
                $v['use_fee']      = $this->record_model->storeRecordFee($v['id']);

                $use_fee           = $this->record_model->storeUseFee($v['id']);

                $v['excess_fee']   = round($v['order_fee'] - $v['refund_fee'] - $use_fee,2);
                //申请中的退款数量
                $v['refund_count'] = $this->refund_order_model->where(['status'=>1,'store_id'=>$v['id']])->count();

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 11:38
     * @功能说明:支付订单
     */
    public function storeOrder(){

        $input = $this->_input;

        $data  = $this->model->adminOrderList($input['store_id'],$input['limit'],$input['title']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 11:38
     * @功能说明:支付订单
     */
    public function storeRefundOrder(){

        $input = $this->_input;

        $data  = $this->refund_order_model->adminOrderList($input['store_id'],$input['limit'],$input['title']);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2020-11-30 17:15
     * @功能说明:提现列表
     */
    public function cashOrderList(){


        $input = $this->_input;

        $dis[] = ['a.uniacid','=',$this->_uniacid];



    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 17:48
     * @功能说明:同意退款
     */
    public function passRefundOrder(){

        $input = $this->_input;

        $refund_order = $this->refund_order_model->dataInfo(['id'=>$input['id']]);

        if($refund_order['status']!=1){

            $this->errorMsg('状态错误');
        }

        $order = $this->model->dataInfo(['id'=>$refund_order['order_id']]);

        Db::startTrans();

        $res = $this->refund_order_model->where(['id'=>$input['id']])->update(['status'=>2]);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('退款失败');
        }

        $response = orderRefundApi($this->payConfig(),$order['pay_price'],$refund_order['refund_price'],$order['transaction_id']);

        if ( isset( $response[ 'return_code' ] ) && isset( $response[ 'result_code' ] ) && $response[ 'return_code' ] == 'SUCCESS' && $response[ 'result_code' ] == 'SUCCESS' ) {


        }else{

            //失败就报错
            $discption = !empty($response['err_code_des'])?$response['err_code_des']:$response['return_msg'];

            Db::rollback();

            $this->errorMsg($discption);

        }


        Db::commit();

        return $this->success(1);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 17:57
     * @功能说明:不同意退款
     */
    public function noPassRefundOrder(){

        $input = $this->_input;

        $refund_order = $this->refund_order_model->dataInfo(['id'=>$input['id']]);

        if($refund_order['status']!=1){

            $this->errorMsg('状态错误');
        }

        $res = $this->refund_order_model->where(['id'=>$input['id']])->update(['status'=>-1]);

        if($res!=1){

            $this->errorMsg('审核');
        }

        return $this->success(1);

    }





}
